package com.ga.wact.service.Impl;

import com.ga.wact.common.PageBean;
import com.ga.wact.dao.*;
import com.ga.wact.entity.*;
import com.ga.wact.service.FormOnePersonInformationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * Created by 幻夜~星辰 on 2018/10/14.
 */
@Service
@Slf4j
public class FormOnePersonInformationServiceImpl implements FormOnePersonInformationService{

    @Autowired
    private FormOnePersonInformationDao formOnePersonInformationDao;

    @Autowired
    private FormOneMaritalInformationDao formOneMaritalInformationDao;

    @Autowired
    private FormOneAssociationInformationDao formOneAssociationInformationDao;

    @Autowired
    private FormOneJoinArmyInformationDao formOneJoinArmyInformationDao;

    @Autowired
    private FormOneWorkingExperienceDao formOneWorkingExperienceDao;

    @Autowired
    private  FormOneHaveVisitedCountryDao formOneHaveVisitedCountryDao;


    @Override
    @Transactional
    public int insert(FormOnePersonInformation formOnePersonInformation) {
        int count=0;
        //根据注册信息，获取userId，添加到类中，再储存到数据库

        //点击保存但不提交的处理,将必须要填的字段进行一些默认处理
        if(formOnePersonInformation.getStatus()==0) {
            //查看之前是否有保存的信息
            if (formOnePersonInformationDao.ifExist(formOnePersonInformation.getUserId()) != null) {
                formOnePersonInformationDao.delBeforeInfo(formOnePersonInformation.getUserId());
            }
            //在选 了护照状态为否，或者未选时，设置有关护照的属性为null,防止信息错误
            if (formOnePersonInformation.getPublicPassportStatus()==0){
                formOnePersonInformation.setPublicPassportOther("");
                formOnePersonInformation.setExpiryDate(null);
                formOnePersonInformation.setPublicPassportEndTime(null);
                formOnePersonInformation.setPublicPassportNumber("");
                formOnePersonInformation.setPassportStatus(0);
            }
            //在选择了是否申请过该国签证的情况下选择了否或者未选，设置有关属性为null
            if (formOnePersonInformation.getHasApplyThisCountryVisa()==0){
                formOnePersonInformation.setHasApplyDate(null);
                formOnePersonInformation.setPlace("");
                formOnePersonInformation.setHasApplyType("");
                formOnePersonInformation.setHasRefuse(0);
                formOnePersonInformation.setGoTime(null);
                formOnePersonInformation.setGoDays(0);
            }
            //在选择了申请过该国签证，但被拒签
            if (formOnePersonInformation.getHasApplyThisCountryVisa()==1&&formOnePersonInformation.getHasRefuse()==0){
                formOnePersonInformation.setGoTime(null);
                formOnePersonInformation.setGoDays(0);
            }
            count = formOnePersonInformationDao.insert(formOnePersonInformation);
            List<FormOneAssociationInformation> formOneAssociationInformations = formOnePersonInformation.getFormOneAssociationInformations();
            if (formOneAssociationInformations!=null){
                for (FormOneAssociationInformation formOneAssociationInformation : formOneAssociationInformations) {
                    formOneAssociationInformation.setFormId(formOnePersonInformation.getId());
                    //当其中属性为空时，去掉
                    if(formOneAssociationInformation.getAssociationName()==null){
                        formOneAssociationInformations.remove(formOneAssociationInformation);
                    }
                    System.out.println(formOneAssociationInformation.getAssociationName());
                }
                if (formOneAssociationInformations.size()>0&&formOneAssociationInformations!=null){
                    formOneAssociationInformationDao.insert(formOneAssociationInformations);
                }
            }



            List<FormOneHaveVisitedCountry> formOneHaveVisitedCountries = formOnePersonInformation.getFormOneHaveVisitedCountries();
            if (formOneHaveVisitedCountries!=null){
                for (FormOneHaveVisitedCountry formOneHaveVisitedCountry : formOneHaveVisitedCountries) {
                    formOneHaveVisitedCountry.setFormId(formOnePersonInformation.getId());
                    //当有属性为空时，去掉该记录
                    if (formOneHaveVisitedCountry.getCountryName()==null){
                        formOneHaveVisitedCountries.remove(formOneHaveVisitedCountry);
                    }
                }

                if (formOneHaveVisitedCountries.size()>0){
                    formOneHaveVisitedCountryDao.insert(formOneHaveVisitedCountries);
                }
            }


            List<FormOneJoinArmyInformation> formOneJoinArmyInformations = formOnePersonInformation.getFormOneJoinArmyInformations();
            if (formOneJoinArmyInformations!=null){
                for (FormOneJoinArmyInformation formOneJoinArmyInformation : formOneJoinArmyInformations) {
                    formOneJoinArmyInformation.setFormId(formOnePersonInformation.getId());
                    if (formOneJoinArmyInformation.getCountry()==null){
                        formOneJoinArmyInformations.remove(formOneJoinArmyInformation);
                    }
                }
                //选择了参军的情况下
                if (formOneJoinArmyInformations.size()>0&&formOnePersonInformation.getJoinArmyStatus()==1&&formOneJoinArmyInformations!=null){
                    formOneJoinArmyInformationDao.insert(formOneJoinArmyInformations);
                }

            }

            FormOneMaritalInformation formOneMaritalInformation = formOnePersonInformation.getFormOneMaritalInformation();
            if (formOneMaritalInformation!=null){
                formOneMaritalInformation.setFormId(formOnePersonInformation.getId());
                //判断各属性是否为空
                if (formOneMaritalInformation.getSpouseName()!=null&&
                        formOneMaritalInformation.getChildBirthPlace()!=null&&
                        formOneMaritalInformation.getSpouseBirthDate()!=null&&
                        formOneMaritalInformation.getChildBirthDate()!=null&&
                        formOneMaritalInformation.getChildName()!=null&&
                        formOneMaritalInformation.getSpouseBirthPlace()!=null){
                    formOneMaritalInformationDao.insert(formOneMaritalInformation);
                }
            }

            List<FormOneWorkExperience> formOneWorkExperiences = formOnePersonInformation.getFormOneWorkExperiences();
            if (formOneWorkExperiences!=null){
                for (FormOneWorkExperience formOneWorkExperience : formOneWorkExperiences) {
                    formOneWorkExperience.setFormId(formOnePersonInformation.getId());
                    if (formOneWorkExperience.getCompanyName()==null){
                        formOneWorkExperiences.remove(formOneWorkExperience);
                    }
                }
                if (formOneWorkExperiences.size()>0&&formOneWorkExperiences!=null){
                    formOneWorkingExperienceDao.insert(formOneWorkExperiences);
                }
            }



        }else if(formOnePersonInformation.getStatus()==1){
            if (formOnePersonInformationDao.ifExist(formOnePersonInformation.getUserId()) != null) {
                formOnePersonInformationDao.delBeforeInfo(formOnePersonInformation.getUserId());
            }

            //在选 了护照状态为否，或者未选时，设置有关护照的属性为null,防止信息错误
            if (formOnePersonInformation.getPublicPassportStatus()==0){
                formOnePersonInformation.setPublicPassportOther(null);
                formOnePersonInformation.setExpiryDate(null);
                formOnePersonInformation.setPublicPassportEndTime(null);
                formOnePersonInformation.setPublicPassportNumber(null);
                formOnePersonInformation.setPassportStatus(0);
            }
            //在选择了是否申请过该国签证的情况下选择了否或者未选，设置有关属性为null
            if (formOnePersonInformation.getHasApplyThisCountryVisa()==0){
                formOnePersonInformation.setHasApplyDate(null);
                formOnePersonInformation.setPlace(null);
                formOnePersonInformation.setHasApplyType(null);
                formOnePersonInformation.setHasRefuse(0);
                formOnePersonInformation.setGoTime(null);
                formOnePersonInformation.setGoDays(0);
            }
            //在选择了申请过该国签证，但被拒签
            if (formOnePersonInformation.getHasApplyThisCountryVisa()==1&&formOnePersonInformation.getHasRefuse()==0){
                formOnePersonInformation.setGoTime(null);
                formOnePersonInformation.setGoDays(0);
            }
            count = formOnePersonInformationDao.insert(formOnePersonInformation);

            //当点击为提交的时候，检查其中有没有为空的数据，其中一个未填代表整个都未填，不插入数据库
            List<FormOneAssociationInformation> formOneAssociationInformations = formOnePersonInformation.getFormOneAssociationInformations();
            if (formOneAssociationInformations!=null){
                for (FormOneAssociationInformation formOneAssociationInformation : formOneAssociationInformations) {
                    if (formOneAssociationInformation.getAssociationName()==""){
                        formOneAssociationInformations.remove(formOneAssociationInformation);
                    }
                    formOneAssociationInformation.setFormId(formOnePersonInformation.getId());
                }
                if (formOneAssociationInformations.size()>0){
                    formOneAssociationInformationDao.insert(formOneAssociationInformations);
                }
            }


            List<FormOneHaveVisitedCountry> formOneHaveVisitedCountries = formOnePersonInformation.getFormOneHaveVisitedCountries();
            if (formOneHaveVisitedCountries!=null){
                for (FormOneHaveVisitedCountry formOneHaveVisitedCountry : formOneHaveVisitedCountries) {
                    formOneHaveVisitedCountry.setFormId(formOnePersonInformation.getId());
                    if (formOneHaveVisitedCountry.getCountryName()==""){
                        formOneHaveVisitedCountries.remove(formOneHaveVisitedCountry);
                    }
                }
                if (formOneHaveVisitedCountries.size()>0){
                    formOneHaveVisitedCountryDao.insert(formOneHaveVisitedCountries);
                }
            }


            List<FormOneJoinArmyInformation> formOneJoinArmyInformations = formOnePersonInformation.getFormOneJoinArmyInformations();
            if (formOneJoinArmyInformations!=null){
                for (FormOneJoinArmyInformation formOneJoinArmyInformation : formOneJoinArmyInformations) {
                    formOneJoinArmyInformation.setFormId(formOnePersonInformation.getId());
                    if (formOneJoinArmyInformation.getCountry()==""){
                        formOneJoinArmyInformations.remove(formOneJoinArmyInformation);
                    }
                }
                if (formOneJoinArmyInformations.size()>0&&formOnePersonInformation.getJoinArmyStatus()==1){
                    formOneJoinArmyInformationDao.insert(formOneJoinArmyInformations);
                }
            }


            FormOneMaritalInformation formOneMaritalInformation = formOnePersonInformation.getFormOneMaritalInformation();
            if (formOneMaritalInformation!=null){
                if (formOneMaritalInformation.getSpouseName()!=""){
                    formOneMaritalInformation.setFormId(formOnePersonInformation.getId());
                    formOneMaritalInformationDao.insert(formOneMaritalInformation);
                }
            }




            List<FormOneWorkExperience> formOneWorkExperiences = formOnePersonInformation.getFormOneWorkExperiences();
            if (formOneWorkExperiences!=null){
                for (FormOneWorkExperience formOneWorkExperience : formOneWorkExperiences) {
                    formOneWorkExperience.setFormId(formOnePersonInformation.getId());
                    if (formOneWorkExperience.getCompanyName()==""){
                        formOneWorkExperiences.remove(formOneWorkExperience);
                    }
                }
                if (formOneWorkExperiences.size()>0){
                    formOneWorkingExperienceDao.insert(formOneWorkExperiences);
                }
            }

        }
            //           理解错误，未填数据的值为“”不为null，只有字段缺失才是这个
//           判断需要填写的字段名是否为空，如果为空，进行默认处理
//            //设置默认姓名
//            if(formOnePersonInformation.getName()==null){
//                formOnePersonInformation.setName("null");
//            }
//            //设置默认拼音名
//            if(formOnePersonInformation.getPinyinName()==null){
//                formOnePersonInformation.setPinyinName("null");
//            }
//            //设置默认别名
//            if(formOnePersonInformation.getAlias()==null){
//                formOnePersonInformation.setAlias("null");
//            }
//            //设置默认性别
//            if(formOnePersonInformation.getGender()!=0||formOnePersonInformation.getGender()!=1){
//                formOnePersonInformation.setGender(-1);
//            }
//            //设置默认身高,未填写默认为0
//
//            //设置默认出生日期
//            if(formOnePersonInformation.getBirthDate()==null){
//                try {
//                    formOnePersonInformation.setBirthDate(StringToYear("2018-10-10"));
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//            //设置默认出生地
//            if(formOnePersonInformation.getBirthPlace()==null){
//                formOnePersonInformation.setBirthPlace("null");
//            }
//            //设置默认身份证号码
//            if(formOnePersonInformation.getIdNumber()==null){
//                formOnePersonInformation.setIdNumber("null");
//            }
//
//            //设置默认父亲姓名
//            if(formOnePersonInformation.getFatherName()==null){
//                formOnePersonInformation.setFatherName("null");
//            }
//            //设置默认母亲姓名
//            if(formOnePersonInformation.getMotherName()==null){
//                formOnePersonInformation.setMotherName("null");
//            }
//            //设置默认本人单位名称
//            if(formOnePersonInformation.getCompanyName()==null){
//                formOnePersonInformation.setCompanyName("null");
//            }
//            //设置默认职务
//            if(formOnePersonInformation.getDuty()==null){
//                formOnePersonInformation.setDuty("null");
//            }
//            //设置默认居住地邮编
//
//
//            //设置默认详细地址
//            if(formOnePersonInformation.getCompanyAddress()==null){
//                formOnePersonInformation.setCompanyAddress("null");
//            }
//            //设置默认公司电话
//            if(formOnePersonInformation.getCompanyTel()==null){
//                formOnePersonInformation.setCompanyTel("null");
//            }
//            //设置默认公司传真
//            if(formOnePersonInformation.getCompanyFax()==null){
//                formOnePersonInformation.setCompanyFax("null");
//            }
//            //设置家庭详细地址
//            if(formOnePersonInformation.getHomeDetailedAddress()==null){
//                formOnePersonInformation.setHomeDetailedAddress("null");
//            }
//            //设置默认家庭电话号码
//            if(formOnePersonInformation.getHomePhone()==null){
//                formOnePersonInformation.setHomePhone("null");
//            }
//            //传呼可以为空，不设置默认值
//
//            //设置默认本人手机号码
//            if(formOnePersonInformation.getCellPhoneNumber()==null){
//                formOnePersonInformation.setCellPhoneNumber("null");
//            }
//            //设置默认电子邮箱
//            if(formOnePersonInformation.getEmail()==null){
//                formOnePersonInformation.setEmail("null");
//            }
//            //设置默认预定出发日期
//            if(formOnePersonInformation.getScheduledDepartureDate()==null){
//                try {
//                    formOnePersonInformation.setScheduledDepartureDate(StringToYear("2018-10-10"));
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//            //设置默认回国日期
//            if(formOnePersonInformation.getReturnDate()==null){
//                try {
//                    formOnePersonInformation.setReturnDate(StringToYear("2018-10-10"));
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//            //设置默认入境地点
//            if(formOnePersonInformation.getEntryPlace()==null){
//                formOnePersonInformation.setEntryPlace("null");
//            }
//
//
//            //设置默认的发照日期
//            if (formOnePersonInformation.getExpiryDate()==null){
//                try {
//                    formOnePersonInformation.setExpiryDate(StringToYear("2018-10-10"));
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//            //设置默认截止日期
//            if (formOnePersonInformation.getPublicPassportEndTime()==null){
//                try {
//                    formOnePersonInformation.setPublicPassportEndTime(StringToYear("2018-10-10"));
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//            //设置默认护照号码
//            if(formOnePersonInformation.getPublicPassportNumber()==null){
//                formOnePersonInformation.setPublicPassportNumber("null");
//            }
//
//
//
//            //设置是否申请过该国签证下的信息，当为1的时候设置
//            if (formOnePersonInformation.getHasApplyThisCountryVisa()==1){
//                if(formOnePersonInformation.getHasApplyDate()==null){
//                    try {
//                        formOnePersonInformation.setHasApplyDate(StringToYear("2018-10-10"));
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//                if (formOnePersonInformation.getPlace()==null){
//                    formOnePersonInformation.setPlace("null");
//                }
//                if (formOnePersonInformation.getHasApplyType()==null){
//                    formOnePersonInformation.setHasApplyType("null");
//                }
//                //在被拒签的情况下
//                if(formOnePersonInformation.getHasRefuse()==1){
//                    if(formOnePersonInformation.getHasRefuseDate()==null){
//                        try {
//                            formOnePersonInformation.setHasRefuseDate(StringToYear("2018-10-10"));
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    }
//                }
//            }
//            //设置去过该国的默认情况，选否或者没填就不用设置
//            if (formOnePersonInformation.getHasGone()==1){
//                if (formOnePersonInformation.getGoTime()==null){
//                    try {
//                        formOnePersonInformation.setGoTime(StringToYear("2018-10-10"));
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//            //设置是否在该国有亲戚
//            if (formOnePersonInformation.getRelationAndVisaStatus()==null){
//                formOnePersonInformation.setRelationAndVisaStatus("null");
//            }


//            count=formOnePersonInformationDao.insert(formOnePersonInformation);
//        }else if(formOnePersonInformation.getStatus()==1){//点击提交按钮的情况
//            if(formOnePersonInformationDao.ifExist(formOnePersonInformation.getUserId())!=null){
//                formOnePersonInformationDao.delBeforeInfo(formOnePersonInformation.getUserId());
//            }
//            count=formOnePersonInformationDao.insert(formOnePersonInformation);
//        }

        return count;
    }


    @Override
    public PageBean<FormOnePersonInformation> selectAll(HashMap hashMap) {

        PageBean<FormOnePersonInformation> pageBean=new PageBean<FormOnePersonInformation>();

        //得到当前页数
        int currPage=(int)hashMap.get("currPage");
        pageBean.setCurrPage(currPage);

        //默认显示页数
        int pageSize=5;
        pageBean.setPageSize(pageSize);
        //得到总的记录数
        int totalCount=formOnePersonInformationDao.selectCount((int)hashMap.get("status"));
        pageBean.setTotalCount(totalCount);

        //封装总页数
        double tc=totalCount;
        Double num=Math.ceil(tc/pageSize);//向上取整
        pageBean.setTotalPage(num.intValue());

        hashMap.put("start",(currPage-1)*pageSize);
        hashMap.put("size",pageSize);


        List<FormOnePersonInformation> formOnePersonInformations=formOnePersonInformationDao.selectAll(hashMap);

        for (FormOnePersonInformation formOnePersonInformation:formOnePersonInformations){
            formOnePersonInformation.setFormOneAssociationInformations(formOneAssociationInformationDao.selectOneInfo(formOnePersonInformation.getId()));
            formOnePersonInformation.setFormOneHaveVisitedCountries(formOneHaveVisitedCountryDao.selectOneInfo(formOnePersonInformation.getId()));
            formOnePersonInformation.setFormOneJoinArmyInformations(formOneJoinArmyInformationDao.selectOneInfo(formOnePersonInformation.getId()));
            formOnePersonInformation.setFormOneMaritalInformation(formOneMaritalInformationDao.selectOneInfo(formOnePersonInformation.getId()));
            formOnePersonInformation.setFormOneWorkExperiences(formOneWorkingExperienceDao.selectOneInfo(formOnePersonInformation.getId()));
        }
        pageBean.setLists(formOnePersonInformations);
        return pageBean;
    }

    @Override
    public FormOnePersonInformation selectOneInfo(int id) {
        FormOnePersonInformation formOnePersonInformation=formOnePersonInformationDao.selectOneInfo(id);
        if (formOnePersonInformation!=null){
            formOnePersonInformation.setFormOneAssociationInformations(formOneAssociationInformationDao.selectOneInfo(id));
            formOnePersonInformation.setFormOneHaveVisitedCountries(formOneHaveVisitedCountryDao.selectOneInfo(id));
            formOnePersonInformation.setFormOneJoinArmyInformations(formOneJoinArmyInformationDao.selectOneInfo(id));
            formOnePersonInformation.setFormOneMaritalInformation(formOneMaritalInformationDao.selectOneInfo(id));
            formOnePersonInformation.setFormOneWorkExperiences(formOneWorkingExperienceDao.selectOneInfo(id));
        }
        return  formOnePersonInformation;
    }
}
